
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "EfficientMap.cc"
#include "TwoWayList.cc"
#include "Types.cc"

class InRecord
{

private:

#include "Atts.cc"

    // Example of what would be in Atts.cc
    /*
      Int n_n_nationkey;
      Str n_n_name;
      Int n_n_regionkey;
      Str n_n_comment;
    */

public:

    InRecord  () {}
    ~InRecord  () {}

    bool ReadIn (FILE *fromMe)
	{
	    bool res;

#include "ReadIn.cc"

	    // Example of what would be in ReadIn.cc
	    /*
	      res = n_n_nationkey.ReadIn(fromMe);
	      res = n_n_name.ReadIn(fromMe);
	      res = n_n_regionkey.ReadIn(fromMe);
	      res = n_n_comment.ReadIn(fromMe);
	    */

	    fgetc_unlocked(fromMe);

	    return res;
	}

    void WriteOut (FILE *toMe)
	{

#include "WriteOut.cc"

	  // Example of what would be in WriteOut.cc
	  /*
	    Int att1 = n_n_nationkey;
	    Str att2 = n_n_name;
	    Int att3 = n_n_regionkey;
	    Str att4 = n_n_comment;
	    att1.WriteOut(toMe);
	    att2.WriteOut(toMe);
	    att3.WriteOut(toMe);
	    att4.WriteOut(toMe);
	  */

	  fputc_unlocked ('\n', toMe);
	}

#include "SortFunctions.cc"
    // Example of what would be included in SortFunctions.cc
    /*
      Int sortFunction1() const { return n_n_nationkey; }
      Str sortFunction2() const { return n_n_name; }
      Int sortFunction3() const { return n_n_regionkey; }
    */

    bool operator < (const InRecord & r2) const
	{

#include "Compare.cc"

	    // example of what would be in Compare.cc
	    /*
	      if (sortFunction1() > r2.sortFunction1()) return true;
	      if (sortFunction1() < r2.sortFunction1()) return false;
	      if (sortFunction2() < r2.sortFunction2()) return true;
	      if (sortFunction2() > r2.sortFunction2()) return false;
	      if (sortFunction3() < r2.sortFunction3()) return true;
	      if (sortFunction3() > r2.sortFunction3()) return false;
	    */

	    return false;
	}
};

// Read all of the records into a vector, sort the vector, and write the
// sorted records to an output file.
int
main(int numArgs, char **args)
{
    if (numArgs != 3)
    {
	fprintf(stderr, "Usage: OrderBy inputFile outputFile\n");
	exit(-1);
    }

    FILE *inFile = fopen(args[1], "r");

    // Initialize vector to hold records
    std::vector<InRecord> records;

    // Read the records and store them in the vector.
    InRecord myRec;
    while (myRec.ReadIn(inFile))
    {
	records.push_back(myRec);
    }

    fclose (inFile);

    // Sort the records
    std::sort(records.begin(), records.end());

    // Write the sorted records out
    FILE *outFile = fopen (args[2], "w");
    for (vector<InRecord>::iterator it = records.begin(); it < records.end(); it++)
    {
	(*it).WriteOut(outFile);
    }

    fclose (outFile);

    return 0;
}
